<?php
/*
 * @Description:
 * @Author: 橘子味的心
 * @Date: 2020-11-12 17:10:31
 * @LastEditTime: 2020-12-17 04:38:27
 * @LastEditors: 橘子味的心
 * @Reference:
 */
namespace app\system\model;

use think\Model;

class AuthRule extends Model
{
    /**
     * 根据条件获取配置列表信息
     */
    public function getAll($limit, $start_time = false, $end_time = false, $key = false, $map = [])
    {
        if (!empty($start_time) && empty($end_time)) {
            $star_time = strtotime($start_time);
            $map[] = ['a.create_time', 'between', [$star_time, time()]];
        }
        if (!empty($start_time) && !empty($end_time)) {
            $star_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $map[] = ['a.create_time', 'between', [$star_time, $end_time]];
        }
        if (!empty($key)) {
            $map[] = ['title|type', 'like', "%" . $key . "%"];
        }
        $res = $this
            ->alias('a')
            ->where($map)
            ->paginate($limit);
        return $res;
    }
    /**
     * 添加/编辑信息
     */
    public function info($param)
    {
        if (!empty($param['id'])) {
            $res = $this::find($param['id']);
        } else {
            $res = $this;
        }
        try {
            $res->save($param);
        } catch (\Exception $e) {
            return false;
        }
        cache('rules', null);
        return true;
    }
    /**
     * 根据id获取配置信息
     * @param $id
     */
    public function getOne($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 状态切换
     */
    public function status($id, $status)
    {
        try {
            $this->where('id', $id)->update(['status' => $status]);
        } catch (\Exception $e) {
            return false;
        }
        return true;
    }
    /**
     * 删除
     */
    public function del($id)
    {
        try {
            $this->where('id', $id)->delete();
        } catch (\Exception $e) {
            return tocode(100, '删除失败');
        }
        return tocode(200, '删除成功');
    }

    /**
     * 批量操作
     */
    public function handleAll($param, $type = 0)
    {
        try {
            //判断是否是批量删除
            if ($type == 2) {
                $this::destroy([$param]);
            } else {
                $this->where('id', 'in', $param)->update(['status' => $type]);
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

}
